<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>singleton模式 &mdash; programming v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="top" title="programming v0.1 documentation" href="../index.html" />
    <link rel="up" title="设计模式学习笔记" href="index.html" />
    <link rel="next" title="command模式" href="command.html" />
    <link rel="prev" title="abstract factory模式" href="abstract_factory.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="command.html" title="command模式"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="abstract_factory.html" title="abstract factory模式"
             accesskey="P">上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="singleton">
<h1>singleton模式<a class="headerlink" href="#singleton" title="永久链接至标题">¶</a></h1>
<div class="section" id="id1">
<span id="index-10"></span><span id="id2"></span><h2>singleton模式定义<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p><strong>Singleton</strong> - Ensure a class only has one instance and provide
a global point to access it.</p>
<p><strong>单例模式</strong> - 保证一个类只有一个实例,并且提供一个全局的访问方法.</p>
</div>
<div class="section" id="id3">
<h2>问题描述<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>在实际中可能某些环境下,只需要 <strong>唯一</strong> 的一个类的实例存在,例如
<strong>线程池</strong>, <strong>缓存</strong>, <strong>对话框</strong>, <strong>处理用户偏好,注册设置的对象</strong>,
<strong>用于日志的对象</strong> 等等.此时就需要使用 <strong>单例模式</strong></p>
<p>单例模式的类图如下:</p>
<img alt="../_images/singleton_class.png" src="../_images/singleton_class.png" />
</div>
<div class="section" id="id4">
<h2>具体代码<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>下载请点击 <a href="../_downloads/singleton.cpp"><strong class="xref">这里</strong></a></p>
<div class="highlight-c++"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46</pre></td><td class="code"><div class="highlight"><pre><span class="cp">#include &lt;iostream&gt;</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Singleton</span>
<span class="p">{</span>
    <span class="k">private</span><span class="o">:</span>
        <span class="n">Singleton</span><span class="p">(){}</span>
        <span class="k">static</span> <span class="n">Singleton</span><span class="o">*</span> <span class="n">pinstance</span><span class="p">;</span>
        <span class="kt">int</span> <span class="n">num</span><span class="p">;</span>

    <span class="k">public</span><span class="o">:</span>
        <span class="k">static</span> <span class="n">Singleton</span><span class="o">*</span> <span class="n">getInstance</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="c">// This is the lazy instantiation, but not thread-safe</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">pinstance</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="c">// here may not be thread-safe, since two threads may enter simultaniously</span>
                <span class="n">pinstance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Singleton</span><span class="p">;</span>
            <span class="k">return</span> <span class="n">pinstance</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="c">// other methods</span>
        <span class="kt">void</span> <span class="n">test</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;NUM:&quot;</span><span class="o">&lt;&lt;</span><span class="n">getNum</span><span class="p">()</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">setNum</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">num</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="kt">int</span> <span class="n">getNum</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">num</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="n">Singleton</span><span class="o">*</span> <span class="n">Singleton</span><span class="o">::</span><span class="n">pinstance</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">Singleton</span> <span class="o">*</span><span class="n">s</span> <span class="o">=</span> <span class="n">Singleton</span><span class="o">::</span><span class="n">getInstance</span><span class="p">();</span>
    <span class="n">s</span><span class="o">-&gt;</span><span class="n">setNum</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>
    <span class="n">s</span><span class="o">-&gt;</span><span class="n">test</span><span class="p">();</span>
    <span class="n">Singleton</span> <span class="o">*</span><span class="n">t</span> <span class="o">=</span> <span class="n">Singleton</span><span class="o">::</span><span class="n">getInstance</span><span class="p">();</span>
    <span class="n">t</span><span class="o">-&gt;</span><span class="n">test</span><span class="p">();</span>  <span class="c">// get the same number 10, which means Single pattern offer a way to keep the global state</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">s</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">t</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>  <span class="c">// and from here, you can find they point to the same address, which from pinstance</span>
     
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>输出结果为:</p>
<div class="highlight-python"><pre>NUM:10
NUM:10
0x8928008
0x8928008</pre>
</div>
</div>
<div class="section" id="id5">
<h2>面对新的需要<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p>只需按需要添加相关的类即可.</p>
</div>
<div class="section" id="id6">
<h2>更多注意<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<p>单例模式本身是很容易理解,但具体实现起来则比较难,需要
注意的点包括:</p>
<ul class="simple">
<li>线程安全</li>
<li>多个 <em>class loaders</em> (例如, java)</li>
<li>因为引入了 <em>static</em> 变量,所以会使程序不是 <em>Reentrant</em></li>
<li>这是个存在争议的模式(维持了一个全局的状态,使用 <em>static</em> 等) ,但是在实际中有大量的使用</li>
<li><strong>Easy understand but hard to impletment</strong></li>
</ul>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>注意线程安全的问题:</p>
<p class="last">在实现 <strong>线程安全</strong> 的单例模式时,可以参考 <strong>参考更多部分</strong></p>
</div>
</div>
<div class="section" id="id7">
<h2>参考更多<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<ol class="arabic simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Reentrant_(subroutine)">Reentrant</a></li>
<li><cite>Thread_safety &lt;http://en.wikipedia.org/wiki/Thread_safety</cite>&gt;`_</li>
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton_Pattern</a></li>
<li><a class="reference external" href="http://www.cppblog.com/dyj057/archive/2005/09/20/346.aspx">C++完美实现Singleton模式</a></li>
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Double_checked_locking_pattern">Double-checked locking</a></li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference external" href="">singleton模式</a><ul>
<li><a class="reference external" href="#id1">singleton模式定义</a></li>
<li><a class="reference external" href="#id3">问题描述</a></li>
<li><a class="reference external" href="#id4">具体代码</a></li>
<li><a class="reference external" href="#id5">面对新的需要</a></li>
<li><a class="reference external" href="#id6">更多注意</a></li>
<li><a class="reference external" href="#id7">参考更多</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="abstract_factory.html"
                                  title="上一章">abstract factory模式</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="command.html"
                                  title="下一章">command模式</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/designpattern/singleton.txt"
                     rel="nofollow">显示源代码</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>快速搜索</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="搜索" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              输入相关的模块，术语，类或者函数名称进行搜索
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="command.html" title="command模式"
             >下一页</a> |</li>
        <li class="right" >
          <a href="abstract_factory.html" title="abstract factory模式"
             >上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" >设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 版权所有 2009, zhutao.iscas@gmail.com.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>